TARGET = gpio_int

C_SOURCES = src/main.c \
src/uart.c \
src/led.c \
src/gic.c \
src/key.c

C_INCLUDES = -Iinc

ASM_SOURCES = start.s

CFLAGS = -c -marm $(C_INCLUDES) -fno-builtin -nostdinc 
PREFIX=arm-linux-gnueabihf-
CC=$(PREFIX)gcc
LD=$(PREFIX)ld
AR=$(PREFIX)ar
SZ=$(PREFIX)size
OBJCOPY=$(PREFIX)objcopy
OBJDUMP=$(PREFIX)objdump

BUILD_DIR = build

OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))


all : $(OBJECTS) | $(BUILD_DIR)
	$(LD) -g -T imx6ull.lds $^ -o $(BUILD_DIR)/$(TARGET).elf
	$(OBJCOPY) $(BUILD_DIR)/$(TARGET).elf -O binary -S $(BUILD_DIR)/$(TARGET).bin
	$(OBJDUMP) -D -m arm $(BUILD_DIR)/$(TARGET).elf > $(BUILD_DIR)/$(TARGET).dis
	$(SZ) $(BUILD_DIR)/$(TARGET).elf
	mkimage -T imximage -n ./tools/imximage.cfg.cfgtmp -e 0x80100000 -d $(BUILD_DIR)/$(TARGET).bin $(BUILD_DIR)/$(TARGET).imx
	dd if=/dev/zero of=$(BUILD_DIR)/1k.bin bs=1024 count=1
	cat $(BUILD_DIR)/1k.bin $(BUILD_DIR)/$(TARGET).imx > $(BUILD_DIR)/$(TARGET).img

$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
	$(CC) $(CFLAGS) -MMD -MP -MF"$(@:%.o=%.d)" $< -o $@

$(BUILD_DIR)/%.o: %.s | $(BUILD_DIR)
	$(CC) $(CFLAGS) -MMD -MP -MF"$(@:%.o=%.d)" $< -o $@
	
$(BUILD_DIR):
	mkdir $@
		
-include $(wildcard $(BUILD_DIR)/*.d)

clean:
	-rm -fR $(BUILD_DIR)